<template>
  <div class="shift-detail">
    <div class="detail-header">
      <h4>班次信息</h4>
      <div class="shift-status">
        <el-tag :type="getStatusType(shiftData.status)" size="small">
          {{ getStatusText(shiftData.status) }}
        </el-tag>
      </div>
    </div>

    <div class="detail-content">
      <!-- 基本信息 -->
      <div class="info-section">
        <h5>基本信息</h5>
        <div class="info-grid">
          <div class="info-item">
            <label>日期：</label>
            <span>{{ formatDate(shiftData.date) }}</span>
          </div>
          <div class="info-item">
            <label>时间：</label>
            <span>{{ shiftData.time }}</span>
          </div>
          <div class="info-item">
            <label>班次类型：</label>
            <el-tag :type="getTypeClass(shiftData.type)" size="small">
              {{ getTypeText(shiftData.type) }}
            </el-tag>
          </div>
          <div class="info-item">
            <label>科室：</label>
            <span>{{ shiftData.department }}</span>
          </div>
          <div class="info-item">
            <label>地点：</label>
            <span>{{ shiftData.location }}</span>
          </div>
        </div>
      </div>

      <!-- 工作统计 -->
      <div class="stats-section">
        <h5>工作统计</h5>
        <div class="stats-grid">
          <div class="stat-item">
            <div class="stat-value">{{ shiftData.maxPatients }}</div>
            <div class="stat-label">最大接诊量</div>
          </div>
          <div class="stat-item">
            <div class="stat-value">{{ shiftData.actualPatients || 0 }}</div>
            <div class="stat-label">已接诊</div>
          </div>
          <div class="stat-item">
            <div class="stat-value">{{ getRemainingPatients() }}</div>
            <div class="stat-label">剩余名额</div>
          </div>
          <div class="stat-item">
            <div class="stat-value">{{ getCompletionRate() }}%</div>
            <div class="stat-label">完成率</div>
          </div>
        </div>
      </div>

      <!-- 注意事项 -->
      <div class="notes-section" v-if="shiftData.notes">
        <h5>注意事项</h5>
        <div class="notes-content">
          {{ shiftData.notes }}
        </div>
      </div>

      <!-- 交接班记录 -->
      <div class="handover-section" v-if="handoverRecords.length > 0">
        <h5>交接班记录</h5>
        <div class="handover-list">
          <div
            v-for="record in handoverRecords"
            :key="record.id"
            class="handover-item"
          >
            <div class="handover-header">
              <span class="handover-time">{{ formatDateTime(record.time) }}</span>
              <el-tag size="small" :type="record.type === 'handover' ? 'primary' : 'success'">
                {{ record.type === 'handover' ? '交班' : '接班' }}
              </el-tag>
            </div>
            <div class="handover-content">
              {{ record.content }}
            </div>
            <div class="handover-doctor">
              医生：{{ record.doctor }}
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="detail-actions">
      <el-button
        v-if="shiftData.status === 'scheduled'"
        type="warning"
        @click="handleRequestChange"
      >
        <el-icon><RefreshRight /></el-icon>
        申请调班
      </el-button>      
      <el-button @click="handleClose">
        <el-icon><Close /></el-icon>
        关闭
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { RefreshRight, VideoPlay, VideoPause, Close } from '@element-plus/icons-vue'

const props = defineProps({
  shiftData: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['request-change', 'close'])

// 模拟交接班记录
const handoverRecords = ref([])

// 方法
const handleRequestChange = () => {
  emit('request-change', props.shiftData)
}


const handleClose = () => {
  emit('close')
}

const getRemainingPatients = () => {
  return props.shiftData.maxPatients - (props.shiftData.actualPatients || 0)
}

const getCompletionRate = () => {
  if (!props.shiftData.actualPatients) return 0
  return Math.round((props.shiftData.actualPatients / props.shiftData.maxPatients) * 100)
}



const getStatusType = (status) => {
  const typeMap = {
    scheduled: 'primary',
    in_progress: 'warning',
    completed: 'success',
    cancelled: 'info'
  }
  return typeMap[status] || 'info'
}

const getStatusText = (status) => {
  const textMap = {
    scheduled: '已安排',
    in_progress: '进行中',
    completed: '已完成',
    cancelled: '已取消'
  }
  return textMap[status] || status
}

const getTypeClass = (type) => {
  const classMap = {
    morning: 'primary',
    afternoon: 'success',
    night: 'warning',
    emergency: 'danger'
  }
  return classMap[type] || 'info'
}

const getTypeText = (type) => {
  const textMap = {
    morning: '早班',
    afternoon: '午班',
    night: '晚班',
    emergency: '急诊'
  }
  return textMap[type] || type
}

const formatDate = (dateString) => {
  if (!dateString) return '-'
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    weekday: 'short'
  })
}

const formatDateTime = (dateTimeString) => {
  if (!dateTimeString) return '-'
  const date = new Date(dateTimeString)
  return date.toLocaleString('zh-CN', {
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}
</script>

<style lang="scss" scoped>
.shift-detail {
  .detail-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 1px solid #ebeef5;

    h4 {
      margin: 0;
      color: #303133;
    }
  }

  .detail-content {
    margin-bottom: 24px;
  }

  .info-section,
  .stats-section,
  .notes-section,
  .handover-section {
    margin-bottom: 24px;

    h5 {
      margin: 0 0 12px 0;
      font-size: 14px;
      font-weight: 600;
      color: #303133;
    }
  }

  .info-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;

    .info-item {
      display: flex;
      align-items: center;

      label {
        font-weight: 500;
        color: #606266;
        min-width: 80px;
        margin-right: 8px;
      }

      span {
        color: #303133;
      }
    }
  }

  .stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
    text-align: center;

    .stat-item {
      padding: 16px;
      background: #f8f9fa;
      border-radius: 8px;

      .stat-value {
        font-size: 20px;
        font-weight: 600;
        color: #409eff;
        margin-bottom: 4px;
      }

      .stat-label {
        font-size: 12px;
        color: #909399;
      }
    }
  }

  .notes-content {
    background: #f8f9fa;
    padding: 12px;
    border-radius: 4px;
    color: #606266;
    line-height: 1.5;
  }

  .handover-list {
    .handover-item {
      padding: 12px;
      border: 1px solid #ebeef5;
      border-radius: 6px;
      margin-bottom: 8px;

      &:last-child {
        margin-bottom: 0;
      }

      .handover-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;

        .handover-time {
          color: #909399;
          font-size: 12px;
        }
      }

      .handover-content {
        color: #303133;
        line-height: 1.5;
        margin-bottom: 8px;
      }

      .handover-doctor {
        font-size: 12px;
        color: #606266;
        text-align: right;
      }
    }
  }

  .detail-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    padding-top: 16px;
    border-top: 1px solid #ebeef5;
  }
}

@media (max-width: 768px) {
  .shift-detail {
    .info-grid {
      grid-template-columns: 1fr;
    }

    .stats-grid {
      grid-template-columns: repeat(2, 1fr);
    }

    .detail-actions {
      flex-direction: column;
    }
  }
}
</style>